/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.b3log.nide;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 * Nide server.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.8, Nov 29, 2010
 */
public final class Nide {

    /**
     * JSON print indent.
     */
    public static final int INDENT = 2;
    /**
     * nide.properties.
     */
    private static final ResourceBundle NIDE_PROPS;
    /**
     * Current operating system is M$ Windows?
     */
    private static final boolean IS_WINDOWS;
    /**
     * Logger.
     */
    private static final Logger LOGGER = Logger.getLogger(Nide.class.getName());

    static {
        IS_WINDOWS =
                System.getProperty("os.name").toUpperCase().contains("WINDOWS");

        if (IS_WINDOWS) {
            NIDE_PROPS = ResourceBundle.getBundle("windows");
        } else {
            NIDE_PROPS = ResourceBundle.getBundle("linux");
        }

        try {
            final ClassLoader classLoader = Thread.currentThread().
                    getContextClassLoader();
            final URL logPropsURL =
                    classLoader.getResource("logging.properties");
            if (logPropsURL == null) {
                throw new Exception("Can't find logging configuration!");
            }

            final File configFile = new File(logPropsURL.toURI());
            System.setProperty("java.util.logging.config.file",
                    configFile.getPath());
            LOGGER.config("Configured logging configuration");
        } catch (final Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Gets nide.properties as a {@link ResourceBundle}.
     *
     * @return nide properties
     */
    public static ResourceBundle getNideProperties() {
        return NIDE_PROPS;
    }

    /**
     * Determines whether the current operating system is Windows.
     *
     * @return returns {@code true} if current operating system is Windows,
     *         {@code false} otherwise
     */
    public static boolean isWindows() {
        return IS_WINDOWS;
    }

    /**
     * Starts nide in embedded jetty.
     *
     * @param args should be empty
     * @throws Exception exception
     */
    public static void main(final String[] args) throws Exception {
        final URL root = Nide.class.getClassLoader().getResource("");
        final String warPath = root + ".." + File.separator + "nide.war";

        final WebAppContext webAppContext = new WebAppContext(warPath, "/");
        final Server server = new Server(8080);
        server.setHandler(webAppContext);

        LOGGER.log(Level.INFO, "Nide runs on {0}", System.getProperty("os.name"));
        LOGGER.log(Level.INFO, "Java home: [{0}]",
                System.getProperty("java.home"));
        server.start();
        server.join();
    }

    /**
     * Private default constructor.
     */
    private Nide() {
    }
}
